package com.fwh.web.utils.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SaltedAuthenticationInfo;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * 
 * @author lqc
 * @date 创建时间：2019年1月7日 上午10:10:27
 * @version
 */
public class ShiroCredentialMatcher extends SimpleCredentialsMatcher {


    /**
     * 传过来的密码与数据库已经加过密的密码进行对比
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        Object tokenCredentials = this.getCredentials(authenticationToken);
        Object accountCredentials = this.getCredentials(authenticationInfo);
        Object salt = ((SaltedAuthenticationInfo) authenticationInfo).getCredentialsSalt();

        // 加盐设置
        Object password = new Md5Hash(tokenCredentials, salt).toString();
        return this.equals(password, accountCredentials);
    }

    /**
     * 得到加密密码
     *
     * @param password 明文密码
     * @return 加密后的密码
     */
    public static String getPassword(String password, String salt) {
        return new Md5Hash(password, salt).toString();
    }
}
